﻿namespace LeetCode_Learn
{
    public class Part06_BSOne
    {
        public static int Get(int[] arr, int num)
        {
            if (arr == null)
                throw new ArgumentNullException("arr不可为空!");
            if (arr.Length < 2)
                return arr[0];

            return binaryFindNum(arr,0, arr.Length - 1,num);
        }

        public static int binaryFindNum(int[] arr, int left, int right, int num)
        {
            if (left > right)//递归结束
                return -1;
            int mid = (left + right) / 2;
            int midVal = arr[mid];
            if (num > midVal)
                return binaryFindNum(arr, mid + 1, right, num);//向右递归
            else if (num < midVal)
                return binaryFindNum(arr, left, mid - 1, num);//向左递归
            else
                return mid;
        }
    }
}
